package com.karson.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.karson.common.model.Author;
import com.karson.common.response.CommonResult;
import com.karson.service.service.AuthorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/author")
@Tag(name = "作者接口")
public class AuthorController {

    @Autowired
    private AuthorService authorService;

    @Operation(summary = "全部列表（按workCount排序，前端使用虚拟化技术）")
    @GetMapping("/list")
    public CommonResult<List<Author>> getList(
            @RequestParam(value = "sortDesc", required = false, defaultValue = "true") boolean sortDesc
    ) {
        LambdaQueryWrapper<Author> queryWrapper = Wrappers.lambdaQuery();
        if (sortDesc) {
            queryWrapper.orderByDesc(Author::getWorkCount);
        } else {
            queryWrapper.orderByAsc(Author::getWorkCount);
        }

        List<Author> list = authorService.list(queryWrapper);
        return CommonResult.success(list);
    }
}
